package edu.bw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import edu.bw.dao.UserDao;
import edu.bw.dao.impl.UserDaoImpl;
import edu.bw.dto.InsertUserForm;
import edu.bw.dto.SelectUserByConditionForm;
import edu.bw.dto.UpdateUserForm;
import edu.bw.pojo.User;
import edu.bw.service.UserService;
import edu.bw.utils.PageUtils;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();//Service中所有的方法都能够使用userDao

    @Override
    public Integer userLogin(User user) {
        return userDao.userLogin(user);
    }

    @Override
    public PageUtils selectUserByCondition(SelectUserByConditionForm selectCondition) {
        List<Map<String,Object>> list = userDao.selectUserByCondition(selectCondition);

        Long totalCount = userDao.selectUserByConditionCount(selectCondition);

        int beginIndex = (selectCondition.getPage() - 1) * selectCondition.getLength();

        return new PageUtils(list,totalCount,beginIndex,selectCondition.getLength());
    }

    @Override
    public Integer insert(InsertUserForm insertUserForm) {
        User user = BeanUtil.copyProperties(insertUserForm, User.class);
        return userDao.insert(user);
    }

    @Override
    public List<Map<String, Object>> selectById(Integer userId) {
        return userDao.selectById(userId);
    }

    @Override
    public Integer update(UpdateUserForm updateUserForm) {
        User user = new User();
        BeanUtil.copyProperties(updateUserForm, user);
        user.setId(updateUserForm.getUserId());

        return userDao.update(user);
    }

    @Override
    public Integer deleteUserByIds(List<Integer> ids) {
        return userDao.deleteUserByIds(ids);
    }
}
